package com.facedemo.utils;

public class ContinuousCountWork {
    private int countNum = 0;
    private int countEndNum = 10;
    private long continuousRange = 1500;
    private long continuousTimeTag = 0;

    private long previousDoWorkTimeTag = 0;
    private long workInterval = 4000;// doWork 的最小间隔

    public ContinuousCountWork(int countEndNum, long continuousRange, long workInterval) {
        this.countEndNum = countEndNum;
        this.continuousRange = continuousRange;
        this.workInterval = workInterval;
    }

    public void count() {
        if(continuousTimeTag==0) {
            continuousTimeTag = System.currentTimeMillis();
        }
        if (System.currentTimeMillis() - continuousTimeTag < continuousRange) {//连续
            countNum++;
        } else {
            countNum = 1;
        }
        continuousTimeTag = System.currentTimeMillis();

        if (countNum >= countEndNum && (System.currentTimeMillis() - previousDoWorkTimeTag > workInterval)) {
            countNum = 0;
            previousDoWorkTimeTag = System.currentTimeMillis();
            doWork();
        }
    }

    public void clearCount() {
        countNum = 0;
        continuousTimeTag = 0;
    }

    public void setCountEndNum(int countEndNum) {
        this.countEndNum = countEndNum;
    }
    public void setContinuousRange(long continuousRange) {
        this.continuousRange = continuousRange;
    }
    public void setWorkInterval(long workInterval) {
        this.workInterval = workInterval;
    }

    public void doWork() {

    }
}
